<?php
class ReportController extends Zend_Controller_Action
{

	public function init()
	{
		//get user session
		$auth = Zend_Auth::getInstance();
		
		//check if user is logged in, otherwise redirect to home page
		if($auth->hasIdentity()){
			$userID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
// 		$this->checkAdmin();
	}
	private function checkAdmin(){
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			$userRank = $auth->getIdentity()->getUserRank();
			if($userRank<2){
// 				$this->_helper->redirector('index','index');
				return false;
			}
			return true;
		}else{
// 			$this->_helper->redirector('index','index');
			return false;
		}
	}
	
	public function courseplacementAction(){
		$cq = new Application_Model_CoursesQuery();
		$t = new Application_Model_Term();
		if($this->getRequest()->getParam('term')){
			if($this->checkAdmin()){
				$data = $cq->getCoursesForTerm_Admin('\''.$this->getRequest()->getParam('term').'\'');
			}
			else{
				$auth = Zend_Auth::getInstance();
				$data = $cq->getCoursesForTerm_Teacher($auth->getIdentity()->getUID(), $this->getRequest()->getParam('term'));
			}
			$this->view->term = $this->getRequest()->getParam('term');
		}
		else{
			if($this->checkAdmin()){
				$data = $cq->getCoursesForTerm_Admin('\''.$t->getCurrentCalendarTermStr().'\'');
			}
			else{
				$auth = Zend_Auth::getInstance();
				$data = $cq->getCourses_Teacher($auth->getIdentity()->getUID());
			}
			$this->view->term = $t->getCurrentOpenTermStr('admnCourse');
		}
		$courses = Array();
		if($data){
			foreach($data as $course){
				$c = Array();
				$c['COURSENAME'] = $course['COURSENAME'];
				$c['COURSENUMBER'] = $course['DEPARTMENT'].$course['COURSENUMBER'];
				$teachers = new Application_Model_DbTable_Teachers();
				$c['TEACHER'] = $teachers->getTeacher('ID = '.$course['TEACHERID']);
				$ca = new Application_Model_CourseAssociationQuery();
				$associations = $ca->selectCAs_Course($course['CID']);
				$positions = Array();
							foreach($associations as $assoc){
					$pos = Array();
					$vrq = new Application_Model_VolunteerQuery();
					$contactsQuery = new Application_Model_ContactsQuery();
					$vpos = $vrq->selectVR_ID($assoc['RID']);
					$contact = $contactsQuery->getPrimaryContact_VR($assoc['RID']);
					$pos['RID'] = $assoc['RID'];
					$pos['PARTNER']=$vrq->getPartnerNameForVRID($assoc['RID']);
					$pos['POSNAME']=$vpos[0]['POSITIONTITLE'];
					$pos['GRANTED']=$assoc['NUMPOSITIONS'];
					$pos['CONTACTNAMEPRE']=$contact['PREFIX'];
					$pos['CONTACTNAMEFIR']=$contact['FIRSTNAME'];
					$pos['CONTACTNAMEMID']=$contact['MIDDLENAME'];
					$pos['CONTACTNAMELAS']=$contact['LASTNAME'];
					$pos['CONTACTNAMESUF']=$contact['SUFFIX'];
					$pos['CONTACTPHONE']=$contact['PHONE'];
					$pos['CONTACTEMAIL']=$contact['EMAIL'];
					$pos['CONTACTADDR1'] = $contact['ADDRESS1'];;
					$pos['CONTACTADDR2'] = $contact['ADDRESS2'];
					$pos['CONTACTADDR3'] = $contact['ADDRESS3'];
					$pos['CONTACTSTATE'] = $contact['STATE'];
					$pos['CONTACTCITY'] = $contact['CITY'];
					$pos['CONTACTZIP'] = $contact['ZIP'];
					$positions[] = $pos;
				}
				$c['POSITIONS']=$positions;
				$courses[] = $c;
			}
		}
		$this->view->courses = $courses;
		$this->view->otherTerms = $t->getAllTermsStr();
	}
	
	public function partnercoursesAction(){
		$vq = new Application_Model_VolunteerQuery();
		$t = new Application_Model_Term();
		if($this->getRequest()->getParam('term')){
				$auth = Zend_Auth::getInstance();
				$data = $vq->getVRsByTerm_CP($auth->getIdentity()->getUID(), $this->getRequest()->getParam('term'));
			
			$this->view->term = $this->getRequest()->getParam('term');
		}
		else{
			$auth = Zend_Auth::getInstance();
			$data = $vq->getVRs_CP($auth->getIdentity()->getUID());
			$this->view->term = $t->getCurrentOpenTermStr('cpvol');
		}
		$requests = Array();
		if($data){
			foreach($data as $req){
				$r = Array();
				$r['VRID'] = $req['VRID'];
				$r['POSITIONTITLE'] = $req['POSITIONTITLE'];
				$r['NUMBERREQUESTING'] = $req['NUMBERREQUESTING'];
				$cq = new Application_Model_CoursesQuery();
				$ca = new Application_Model_CourseAssociationQuery();
				$teachers = new Application_Model_DbTable_Teachers();
				$associations = $ca->selectCAs_VR($req['VRID']);
				$courses = Array();
				foreach($associations as $assoc){
					$c = Array();
					$cdata = $cq->getCourse_ID($assoc['CID']);
					$c['CID'] = $assoc['CID'];
					$c['NUMBER']=$cdata['DEPARTMENT'].$cdata['COURSENUMBER'];
					$c['COURSENAME']=$cdata['COURSENAME'];
					$c['TEACHER'] = $teachers->getTeacher('ID = '.$cdata['TEACHERID']);
					$c['GRANTED']=$assoc['NUMPOSITIONS'];
					$courses[]=$c;
				}
				$r['COURSES']=$courses;
				$reqests[] = $r;
			}
		}
		$this->view->requests = $requests;
		$this->view->otherTerms = $t->getAllTermsStr();
	}
	
	
	public function coursesreportAction(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$id = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$reportsQuery = new Application_Model_ReportsQuery();
		$results = $reportsQuery->coursesReport();
		$this->view->data = $results;

	}
	
		public function orghistoryAction(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$id = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$reportsQuery = new Application_Model_ReportsQuery();
		$results = $reportsQuery->orghistoryReport();
		$this->view->data = $results;

	}

	
		public function orgtocourseAction(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$id = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$reportsQuery = new Application_Model_ReportsQuery();
		$results = $reportsQuery->orgtocourseReport();
		$this->view->data = $results;

	}
	
	public function openrequestsAction(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$id = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$reportsQuery = new Application_Model_ReportsQuery();
		$results = $reportsQuery->openrequestsReport();
		$this->view->data = $results;

	}
	
	public function sendmailAction(){
		require_once 'Zend/Mail.php';
		$smtpServer = 'smtp.unc.edu';
		$config = array('port' => '587');

		$transport = new Zend_Mail_Transport_Smtp($smtpServer, $config);


		$mail = new Zend_Mail();

		//$mail->setFrom('dshue@email.unc.edu', 'Some Sender');
		$mail->addTo('dshue@email.unc.edu', 'Some Recipient');
		$mail->setSubject('Test Subject');
		$mail->setBodyText('This is the text of the mail.');

		$mail->send($transport);
	
	}
}
?>
